package com.dek.seckill.common.mq;


import com.dek.seckill.common.util.JsonUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MQProducer {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void produce(Object message) {
        String msg = JsonUtil.toJson(message);
        amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
        System.out.println("produce msg " + msg);
    }


    public void produceTopic(Object message) {
        String msg = JsonUtil.toJson(message);
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, MQConfig.ROUTING_KEY1, msg + "1");
        amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, MQConfig.ROUTING_KEY2, msg + "2");
        System.out.println("produceTopic msg " + msg);
    }

    public void produceFanout(Object message) {
        String msg = JsonUtil.toJson(message);
        amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE,"", msg + "1");
        System.out.println("produceFanout msg " + msg);
    }

    public void produceHeader(Object message) {
        String msg = JsonUtil.toJson(message);
        MessageProperties properties = new MessageProperties();
        properties.setHeader("header1", "val1");
        properties.setHeader("header2", "val2");
        Message headerMsg = new Message(msg.getBytes(), properties);
        amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE,MQConfig.HEADER_QUEUE, headerMsg);
        System.out.println("produceHeader msg " + msg);
    }

    public void produceSeckill(Object message) {
        String msg = JsonUtil.toJson(message);
        amqpTemplate.convertAndSend(MQConfig.SECKILL_QUEUE, msg);
        System.out.println("produce msg " + msg);
    }

}
